import express from "express";
import "module-alias/register";

import router from "./src/routers/index";
import connectToDatabase from "./src/database/connection";
import config from "./src/config/config.default";
import { responseMiddleWare } from "@/middlewares/requests";
const cors = require("cors");
// 创建应用实例
const app = express();
// 连接数据库
connectToDatabase();
// 使用 express 自带的解析器
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// 注入路由
app.use(
  cors({
    origin: "*",
    // origin: "http://admin.laizhiyu.cn",
    methods: "GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS",
    allowedHeaders: "Content-Type,Authorization",
    credentials: true, // 如果需要传递凭据
  })
);

app.use(router);
app.use(responseMiddleWare);
// 获取端口
const port = process.env.PORT || config.projectOptions.port;
// 启动应用
app.listen(port, () => {
  console.log("应用启动");
  console.log(`Listening on port ${port}`);
});
